package com.qqtech.qquan.circle.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.dao.CircleInfoDao;
import com.qqtech.qquan.circle.dao.CircleMemberDao;
import com.qqtech.qquan.circle.dao.CircleTopicDao;
import com.qqtech.qquan.circle.enums.CircleTopicBizzTypeDictEnum;
import com.qqtech.qquan.circle.enums.CircleTopicOptTypeEnum;
import com.qqtech.qquan.circle.enums.CircleTopicStatusDictEnum;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.circle.model.CircleTopicQuery;
import com.qqtech.qquan.circle.model.CircleTopicVo;
import com.qqtech.qquan.hi.service.HiCircleService;

@Service
public class CircleTopicServiceImpl extends BaseServiceImpl<CircleTopic>implements CircleTopicService {
	public static final Logger log = LoggerFactory.getLogger(CircleTopicServiceImpl.class);
	@Resource
	private CircleBaseService circleBaseService;
	@Resource
	private CircleMemberService circleMemberService;
	
	@Resource
	private CircleTopicDao circleTopicDao;

	@Resource
	private CircleInfoDao circleInfoDao;
	
	@Resource
	private CircleMemberDao circleMemberDao;
	@Resource
	private HiCircleService hiCircleService;

	@Override
	protected BaseDao<CircleTopic> getBaseDao() {
		return circleTopicDao;
	}

	@Override
	public List<CircleTopicVo> queryCircleTopics(int circleId) {
		CircleTopic topic = new CircleTopic();
		topic.setCircleId(circleId);// 圈子id
		topic.setIsShow(YesNoEnum.YES.getCode());
		return circleTopicDao.queryList(topic);
	}

	@Override
	public List<CircleTopicVo> queryMyCircleTopics(Integer userId) {
		CircleTopic topic = new CircleTopic();
		topic.setCreatorId(userId);// 话题创建者id
		topic.setIsShow(YesNoEnum.YES.getCode());
		return circleTopicDao.queryList(topic);
	}

	@Override
	public int hideCircleTopics(int circleId, Integer optUserId) {
		CircleTopic topic = new CircleTopic();
		topic.setCircleId(circleId);// 圈子id
		topic.setIsShow(YesNoEnum.NO.getCode());// 屏蔽
		topic.setOptType(CircleTopicOptTypeEnum.HIDE.getCode());
		topic.setOptUserId(optUserId);
		topic.setOptAt(TimeUtil.now());
		return circleTopicDao.updateByCircleId(topic);
	}

	@Override
	public int showTopic(int topicId, Integer optUserId) {
		CircleTopic topic = new CircleTopic();
		topic.setId(topicId);
		topic.setIsShow(YesNoEnum.YES.getCode());
		topic.setOptType(CircleTopicOptTypeEnum.HIDE.getCode());
		topic.setOptUserId(optUserId);
		topic.setOptAt(TimeUtil.now());
		return circleTopicDao.updateById(topic);
	}

	@Override
	public int hideTopic(int topicId, Integer optUserId) {
		CircleTopic topic = new CircleTopic();
		topic.setId(topicId);
		topic.setIsShow(YesNoEnum.NO.getCode());// 屏蔽
		topic.setOptType(CircleTopicOptTypeEnum.HIDE.getCode());
		topic.setOptUserId(optUserId);
		topic.setOptAt(TimeUtil.now());
		return circleTopicDao.updateById(topic);
	}

	@Override
	public int addTopic(CircleTopic circleTopic) {
		circleTopic.setPv(0);
		circleTopic.setStatus(CircleTopicStatusDictEnum.NORMAL.getCode());
		circleTopic.setIsShow(YesNoEnum.YES.getCode());
		if(null==circleTopic.getBizzId()){
			circleTopic.setBizzType(CircleTopicBizzTypeDictEnum.TOPIC.getCode());
		}
		Integer topicId = this.insert(circleTopic);// 1.保存话题信息

		Integer circleId = circleTopic.getCircleId();
		if (topicId != null && circleId != null) {
			circleInfoDao.increaseTopicCount(circleId, 1);// 圈子话题数量+1
			circleMemberDao.increaseTopicCount(circleId,circleTopic.getCreatorId(), 1);// 成员发布话题数量+1
			circleMemberService.syncLastTopicTime(circleTopic.getCreatorId(), circleId);//更新【最后发布话题的时间】
			hiCircleService.addTopic(circleTopic.getCreatorId(), circleId);//荣誉系统--发布圈子动态
		}
		return topicId;
	}

	@Override
	public boolean isTopicCreator(Integer userId, Integer topicId) {
		CircleTopic topic = new CircleTopic();
		topic.setCreatorId(userId);
		topic.setId(topicId);
		topic.setStatus(CircleTopicStatusDictEnum.NORMAL.getCode());
		return circleTopicDao.queryCount(topic) > 0;
	}

	@Override
	public void deleteTopic(Integer topicId, Integer optUserId) {
		CircleTopic topic = new CircleTopic();
		topic.setId(topicId);
		topic.setStatus(CircleTopicStatusDictEnum.DELETE.getCode());// 2删除状态
		topic.setIsShow(YesNoEnum.NO.getCode());// 隐藏
		topic.setOptType(CircleTopicOptTypeEnum.DELETE.getCode());
		topic.setOptUserId(optUserId);
		topic.setOptAt(TimeUtil.now());
		int updateRow = circleTopicDao.updateById(topic);
		if (updateRow == 1) {
			CircleTopic topicTemp = circleTopicDao.getById(topicId);
			if (topicTemp != null) {
				circleInfoDao.reduceTopicCount(topicTemp.getCircleId(), 1);// 圈子话题数量-1
				circleMemberDao.increaseTopicCount(topicTemp.getCircleId(),topicTemp.getCreatorId(), -1);// 成员发布话题数量-1
			}

		}

	}

	@Override
	public int queryMyTopicCount(Integer userId) {
		CircleTopic topic = new CircleTopic();
		topic.setCreatorId(userId);// 话题创建者id
		topic.setIsShow(YesNoEnum.YES.getCode());
		return circleTopicDao.queryCount(topic);
	}

	@Override
	public int doTop4Topic(int topicId,String tag, Integer optUserId) {
		CircleTopic topic = new CircleTopic();
		topic.setId(topicId);
		topic.setTopAt(TimeUtil.now());
		topic.setIsTop(YesNoEnum.YES.getCode());
		topic.setOptType(CircleTopicOptTypeEnum.TOP.getCode());
		topic.setOptUserId(optUserId);
		topic.setOptAt(TimeUtil.now());
		if(StringUtil.isNotBlank(tag)){
			topic.setTag(tag);
		}
		return circleTopicDao.updateById(topic);
	}

	@Override
	public int undoTop4Topic(int topicId, Integer optUserId) {
		CircleTopic topic = new CircleTopic();
		topic.setId(topicId);
		topic.setTopAt(TimeUtil.now());
		topic.setIsTop(YesNoEnum.NO.getCode());
		topic.setOptType(CircleTopicOptTypeEnum.TOP.getCode());
		topic.setOptUserId(optUserId);
		topic.setOptAt(TimeUtil.now());
		return circleTopicDao.updateById(topic);
	}

	@Override
	public void increasePv(Integer topicId) {
		circleTopicDao.increasePv(topicId, 1);
		
	}
	
	@Override
	public Page<CircleTopicVo> queryPageListAdmin(CircleTopicQuery query, Pageable pageable) {
		return circleTopicDao.queryPageListAdmin(query, pageable);
	}
	
	@Override
	public List<CircleTopicVo> queryList4Admin(CircleTopicQuery query, Pageable pageable) {
		return circleTopicDao.queryList4Admin(query, pageable);
	}
	
	@Override
	public int queryCount4Admin(CircleTopicQuery query) {
		return circleTopicDao.queryCountAdmin(query);
	}
	
	@Override
	public List<Integer> queryCreatorIds(Integer circleId){
		return circleTopicDao.queryCreatorIds(circleId);
	}

	@Override
	public void increaseComment(Integer topicId, int addCount) {
		circleTopicDao.increaseComment(topicId, addCount);
		
	}

	@Override
	public void increasePraise(Integer topicId, int addCount) {
		circleTopicDao.increasePraise(topicId, addCount);
	}

	@Override
	public List<Integer> queryMyTopicCircleIds(Integer userId) {
		return circleTopicDao.queryMyTopicCircleIds(userId);
	}

	@Override
	public boolean isFirstAddTopic(Integer userId) {
		CircleTopicQuery topicQ = new CircleTopicQuery();
		topicQ.setCreatorId(userId);
		return circleTopicDao.queryCount(topicQ) == 0;
	}
	
	@Override
	public Integer getTopicIdByBizzType(Integer bizzId,CircleTopicBizzTypeDictEnum bizzType) {
		if(bizzId==null){
			return null;
		}
		CircleTopicQuery topicQ = new CircleTopicQuery();
		topicQ.setBizzId(bizzId);
		topicQ.setBizzType(bizzType.getCode());
		List<CircleTopicVo> topicList  = circleTopicDao.queryList(topicQ);
		if(topicList!=null && topicList.size()>0){
			return topicList.get(0).getCircleId();
		}else{
			return null;
		}
	}
	
}